<?php
if ( defined('ABSPATH') )
	include_once ABSPATH . 'config.php';
else
    include_once 'config.php';

include_once ABSPATH."base/curl.php";
include_once ABSPATH."base/people.php";
include_once ABSPATH."base/channels.php";

// how many rows to show per page
$rowsPerPage = 20;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_POST['page']))
{
	$pageNum = $_POST['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;


if(isset($_COOKIE["ombid"]))
{
	$id = $_COOKIE["ombid"];
}
else
{
	header("Location: index.php");
}


if(isset($_POST["btnSave"]))
{
	$person = new Person($id);

	if(isset($_POST["chkContact"]))
	{
		foreach($_POST["chkContact"] as $contact)
		{
			$contact_id = Person::IDFromAddress($contact);
			$person->Disconnect($contact_id);
		}
	}
	
	//Disconnect Old
	foreach($_POST["hdnContact"] as $contact)
	{
		    $found = false;
		    if(isset($_POST["hdnContact".$contact]))
		    {
				foreach($_POST["hdnContact".$contact] as $channel)
				{
					
					if(isset($_POST["chk".$contact.$channel]))
					{
						$person->Connect($contact,$channel);
					}
					else
					{
						$person->Disconnect($contact,$channel);
					}
				}
		    }

	
	}

	
}

include_once THEME_PATH."header.php";

?>
<div id="page">
<?php include_once "header.php"; ?>
<div id="content" class="widecolumn">
<input type="hidden" id="id">
<a href="index.php">Home</a>
<ul class="layoutA">
<li>
<form method="post">
<input type="hidden" id="pageNum" name="page" value="<?php echo $pageNum ?>">
<p><input type="text" name="txtSearch" value="<?php echo $_POST["txtSearch"] ?>" /><input type="submit" id="btnSearch" name="btnSearch" value="Search"/></p>
<input type="submit" name="btnSave" value="Update">
<div id="divList">
<?php

//Connect to Directory Server
//SearchText, PageNum, MaxResults
//Reply Unique Address, Name
//Lookup Addresses, Filter if already Connected
//Selected, If local, just connect
//If not local, Request details, insert, then connect


if(isset($_POST["btnSearch"]) || isset($_POST["page"]))
{
	$channels = new Channels();
	$channelList = $channels->Get($id);

	$person = new Person($id);

	$connections = $person->ConnectionsGet($pageNum, $rowsPerPage);
	$numRows = $person->ConnectionsGet($pageNum, $rowsPerPage, true);

	$contactChannel = array();
	if($connections)
	{
		foreach($connections as $conn)
		{
			if(!is_array($contactChannel[$conn["contact_id"]]))
			{
				$contactChannel[$conn["contact_id"]] = array();
			}
			array_push($contactChannel[$conn["contact_id"]],$conn["channel_id"]);
		}
	
		echo '<table style="margin-top:20px">';
		echo '<tr><td><strong>Your Connections</strong></td><td><strong>Disconnect</strong></td></tr>';
		foreach($connections as $contact)
		{
			if($prevContact != $contact["contact_id"])
			{
				$alt = $alt == true ? false:true;
				$prevContact = $contact["contact_id"];
				if(!$alt)
				{
					echo '<tr><td colspan="2"><hr/></td></tr>';
				}
				echo '<tr class="cnxnmgr-row">';
				echo '<td width="300"><a href="javascript:void(0)" onclick="Profile(\''.$contact["address"].'\',DOM(\'divDetail\'))" title="'.$contact["screen_name"].'">'.$contact["name"].'</a></div>';	
				echo '</td><td><input name="hdnContact[]" type="hidden" value="'.$contact["contact_id"].'"/>';
				echo '<input name="chkContact[]" value="'.$contact["address"].'" type="checkbox"></td>';
				echo '<tr><td><div style="margin-left:20px;margin-top:0;">Groups<br/>';
		
				if($channelList)
				{
					echo '<table><tr>';
					$ndx = 0;
					foreach($channelList as $channel)
					{
						$checked = "";
						foreach($contactChannel[$contact["contact_id"]] as $channel2)
		
						if($channel2 == $channel["id"])
						{
							$checked = 'checked="checked" ';
						}
		
						echo '<td><input type="hidden" name="hdnContact'.$contact["contact_id"].'[]" value="'.$channel["id"].'" /><input type="checkbox" '.$checked.' value="'.$channel["id"].'" name="chk'.$contact["contact_id"].$channel["id"].'" /> '. $channel["name"].'</td>';	
						$ndx++;
						if($ndx == 3 )
						{
							echo '</tr><tr>';
							$ndx = 0;
							
						}
					}
					echo '</tr></table>';
				}
				echo "</div></td><td></td></tr>";
			}
		}
		echo '</table>';

		// how many pages we have when using paging?
		$maxPage = ceil($numRows/$rowsPerPage);
		
		
		$self = $_SERVER['PHP_SELF'];
		
		// creating 'previous' and 'next' link
		// plus 'first page' and 'last page' link
		
		// print 'previous' link only if we're not
		// on page one
		if ($pageNum > 1)
		{
			$page = $pageNum - 1;
			$prev = " <a href='javascript:void(0)' onclick='return PeopleListGet($page);'>&lt;</a> ";
			$first = " <a href='javascript:void(0)' onclick='return PeopleListGet(1);'>&lt;&lt;</a> | ";
		} 
		else
		{
			$prev  = ' &lt; ';       // we're on page one, don't enable 'previous' link
			$first = ' &lt;&lt; | '; // nor 'first page' link
		}
		
		// print 'next' link only if we're not
		// on the last page
		if ($pageNum < $maxPage)
		{
			$page = $pageNum + 1;
			$next = " <a href='javascript:void(0)' onclick='return PeopleListGet($page);'>&gt;</a> | ";
			$last = " <a href='javascript:void(0)' onclick='return PeopleListGet($maxPage);'>&gt;&gt;</a> ";
		} 
		else
		{
			$next = ' &gt; | ';      // we're on the last page, don't enable 'next' link
			$last = ' &gt;&gt; '; // nor 'last page' link
		}
		
		// print the page navigation link
		echo $first . $prev . " Showing page <strong>$pageNum</strong> of <strong>$maxPage</strong> pages " . $next . $last;
	}	
	
}

?>
</div>
</form>
</li>
<li>
<div id="divDetail"></div>
</li>
</ul>
</div>
<?php include_once THEME_PATH."footer.php";?>
